//
//  NickNameViewModel.swift
//  Exchange
//
//  Created by rui on 2018/6/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class NickNameViewModel: BaseViewModel {
    
    var nickName: String = ""
    
    private lazy var nickNameApi: NickNameRequest = {
        let nickName = NickNameRequest()
        nickName.dataReformer = self
        nickName.paramSource = self
        nickName.delegate = self
        return nickName
    }()
    
    func uploadNickName(nickName: String) {
        self.nickName = nickName
        self.nickNameApi.loadData()
    }
    
}

extension NickNameViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? NickNameRequest {
            AppInfo.default.nickName = nickName
            self.response?(ResponseState.mine_nickName,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? NickNameRequest {
            self.response?(ResponseState.mine_nickName,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? NickNameRequest {
            pramas.updateValue(nickName, forKey: "nickName")
        }
        return pramas
    }
    
}
